package appapi.controller.oider;

import appapi.Request.Basic.StaffReq;
import appapi.Request.StaffPageReq;
import appapi.Global.BusinessException;
import appapi.common.BaseEntity;
import appapi.common.BusinessTypeEnum;
import appapi.common.ResponseBean;
import appapi.config.MyLog;
import appapi.config.CurrentUser;
import appapi.dto.DelStaffDto;
import appapi.dto.EditPwdDto;
import appapi.dto.ResetPwdDto;
import appapi.entity.Zhongjiao.StaffDto;
import appapi.entity.basic.UserInfo;
import appapi.model.ResponseDataModel;
import appapi.model.ResponseModel;
import appapi.service.IStaffService;
import appapi.utils.StringUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author djq
 * @date 2023/5/10 9:17
 * @description:
 */
@Api(tags = "基础数据-管理人员")
@RestController
@RequestMapping(value = "/api/staff")
public class StaffController {

    @Resource
    private IStaffService iStaffService;

    /**
     * 新增管理人员
     *
     * @return
     * @auther lc 2023/5/10
     */
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @PostMapping(value = "/newStaff")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "staffDto", dataType = "object", value = "管理人员参数")
    })
    @MyLog(title = "新增管理人员",businessType = BusinessTypeEnum.insert)
    public ResponseBean insertStaff(@Validated @RequestBody StaffDto staffDto) {
        try {
            staffDto.setStaffAccount(staffDto.getStaffAccount());
            staffDto.setStaffPwd(staffDto.getStaffPwd());
            iStaffService.InsertStaff(staffDto);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("新增用户异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 修改管理人员
     *
     * @return
     * @auther lc 2023/5/10
     */
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PostMapping(value = "/updateStaff")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "staffDto", dataType = "object", value = "管理人员参数")
    })
    @MyLog(title = "修改管理人员",businessType = BusinessTypeEnum.update)
    public ResponseBean updateStaff(@RequestBody StaffDto staffDto) {
        try {
            iStaffService.UpdateStaff(staffDto);
            return ResponseBean.success();
        } catch (BusinessException exception) {

            return ResponseBean.fail("修改用户异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 删除管理人员
     *
     * @return
     * @auther lc 2023/5/10
     */
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @PostMapping(value = "/delStaff")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "staffId", dataType = "Long", value = "管理人员Id")
    })
    @MyLog(title = "删除管理人员",businessType = BusinessTypeEnum.delete)
    public ResponseBean delStaff(@RequestBody DelStaffDto dto) {
        try {
            if (dto.getStaffId() == null){
                return ResponseBean.fail("数据不能为空!");
            }
            iStaffService.DelStaff(dto.getStaffId());
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("删除用户异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 查询管理人员
     *
     * @return
     * @auther lc 2023/5/10
     */
    @ApiOperation(value = "分页查询管理人员", notes = "分页查询管理人员")
    @GetMapping(value = "/selectStaff")
    public ResponseBean selectStaff(@CurrentUser UserInfo user, StaffPageReq dto) {
        try {
            if (dto.getCurrPage() == 0 || dto.getPageSize() == 0) {
                dto.setCurrPage(1);
                dto.setPageSize(10);
            }
            if (dto.getOrgaPathIds() == null) {
                //拿到人员信息
                Map<String, Object> staffDto = iStaffService.SelectStaffDetail(user.getUserid());
                String orgaPathIds = staffDto.get("orgaPathIds").toString();
                dto.setOrgaPathIds(orgaPathIds);
            }

            List<Map<String, Object>> staffDtos = iStaffService.SelectStaffPage(dto);

            PageInfo pageInfo = new PageInfo(staffDtos, dto.getPageSize());

            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("查询管理人员异常: ".concat(exception.getMessage()));
        }
    }


    /**
     * 查询管理人员
     *
     * @return
     * @auther lc 2023/5/10
     */
    @ApiOperation(value = "查询管理人员选择列表", notes = "查询管理人员选择列表")
    @GetMapping(value = "/selectStaffList")
    public ResponseBean selectStaffList(@CurrentUser UserInfo user,StaffReq req) {
        try {
            if (req.getOrgaPathIds() == null) {
                //获取人员组织机构
                Map<String, Object> staffDto = iStaffService.SelectStaffDetail(user.getUserid());
                String orgaPathIds = staffDto.get("orgaPathIds").toString();
                req.setOrgaPathIds(orgaPathIds);
            }
            List<StaffDto> dataList= iStaffService.SelectStaffList(req);
            return ResponseBean.success(dataList);
        } catch (BusinessException exception) {
            return ResponseBean.fail("查询管理人员异常: ".concat(exception.getMessage()));
        }
    }
    /**
     * 查询管理人员详情
     *
     * @return
     * @auther lc 2023/5/10
     */
    @ApiOperation(value = "查询管理人员详情", notes = "查询管理人员详情")
    @GetMapping(value = "/selectStaffDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "staffId", dataType = "long", value = "人员id")
    })
    public ResponseBean selectStaffDetail(Long staffId) {
        try {
            Map<String, Object> staffDto = iStaffService.SelectStaffDetail(staffId);

           return ResponseBean.success(staffDto);
        } catch (BusinessException exception) {

           return ResponseBean.fail("查询管理人员详情异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 修改密码
     *
     * @return
     * @auther djq 2023/5/10
     */
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping(value = "/editPwd")
    @MyLog(title = "修改密码",businessType = BusinessTypeEnum.update)
    public ResponseBean editPwd(@RequestBody EditPwdDto dto) {
        try {
            iStaffService.UpdatePwd(dto);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("修改密码异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 重置密码
     *
     * @return
     * @auther lc 2023/5/10
     */
    @ApiOperation(value = "重置密码", notes = "重置密码")
    @PostMapping(value = "/resetPwd")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "staffId", dataType = "Long", value = "管理人员Id")
    })
    @MyLog(title = "重置密码",businessType = BusinessTypeEnum.update)
    public ResponseBean resetPwd(@RequestBody ResetPwdDto dto) {
        try {
            Long staffId = dto.getStaffId();
            iStaffService.ResetPwd(staffId);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return  ResponseBean.fail("重置密码异常: ".concat(exception.getMessage()));
        }
    }
}
